<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class CreateDeadlineTaskTable extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function up()
    {
        $table = $this->table('deadline_task', ['id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => 'deadline任务表']);
        $table->addColumn('id', 'integer', ['identity' => true, 'signed' => false, 'limit' => 11, 'comment' => '主键ID'])
            ->addColumn('tenant_id', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '租户id'])
            ->addColumn('type', 'string', ['default' => '', 'limit' => 100,  'comment' => '任务类型'])
            ->addColumn('status', 'integer', ['signed' => false, 'limit' => 255, 'default' => 1, 'comment' => '状态：1=未开始 2=进行中 3=已完成 4=失败 5=重试中 6=预开始'])
            ->addColumn('json', 'json', ['null' => true, 'comment' => '数据'])
            ->addColumn('job_id', 'string', ['default' => '', 'limit' => 100, 'comment' => '唯一标识'])
            ->addColumn('created', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '创建时间'])
            ->addColumn('endtime', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '完成时间'])
            ->addColumn('updated', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '更新时间'])
            ->addIndex(['tenant_id', 'updated', 'status'], ['type' => 'normal', 'name' => 'tenant_id_status'])
            ->save();
    }

    public function down()
    {
        $this->table('deadline_task')->drop()->save();
    }
}
